﻿
@{
    ViewBag.Title = "功能管理 - " + ViewBag.DomainTitle;
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="tree-panel">
    <div id="treeDemo" class="ztree"></div>
    <div class="edit ptop15">
        <div class="layui-fluid">
            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                <legend>已选中 - <span id="selectedName">无</span></legend>
            </fieldset>
        </div>
        <form class="layui-form query-form" id="dataForm">
            <input type="text" id="ID" name="ID" value="" class="layui-hide" />
            <input type="text" id="Pid" name="Pid" value="" class="layui-hide" />
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="Title" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">路径</label>
                    <div class="layui-input-inline">
                        <input type="text" name="Url" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-inline">
                        <input type="text" name="Remark" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">图标</label>
                    <div class="layui-input-inline">
                        <input type="text" name="Img" id="Img" class="layui-input" placeholder="点击上传" readonly>
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">排序</label>
                    <div class="layui-input-inline">
                        <input type="number" name="Sort" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">类型</label>
                    <div class="layui-input-inline">
                        <select name="Type">
                            @foreach (var item in Enum.GetValues(typeof(QunProject.Model.EnumHelper.MenuType)))
                            {
                                <option value="@((int)item)">@item.ToString()</option>
                            }
                        </select>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">是否有效</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="IsValid" lay-skin="switch" lay-text="T|F" checked>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <span class="layui-btn layui-btn-sm" onclick="javascript: { submitNode(); }">保存当前节点</span>
                    <span class="layui-btn layui-btn-sm" onclick="javascript: { submitNodeChild(); }">添加子节点</span>
                    <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm" id="btnReset">重置</button>
                    <span class="layui-btn layui-btn-warm layui-btn-sm" onclick="javascript: { deleteNode(); }">删除</span>

                </div>
            </div>
        </form>
    </div>
</div>

@Html.Partial("~/Views/Shared/_Script.cshtml")
<link href="~/Resources/zTree/css/demo.css" rel="stylesheet" />
<link href="~/Resources/zTree/css/metroStyle/metroStyle.css" rel="stylesheet" />
<script src="~/Resources/zTree/js/jquery.ztree.core.min.js"></script>

<script>
    var form = layui.form;
    var zTreeObj;
    // zTree 的参数配置
    var setting = {
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "pId",
                rootPId: 0
            }
        },
        callback: {
            onClick: zTreeOnClick
        }
    };

    $(function () {
        layui.upload.render({
            elem: '#Img'
            , url: '/Base/Upload'
            , data: { type: 0 }
            , done: function (result) {
                if (result.succ) {
                    $('#Img').val(result.data);
                } else {
                    layer.msg(result.msg);
                }
            }
        });

        bindTree();
    });
    // 绑定树
    function bindTree() {
        $.post('/SysMenu/AjaxData', {}, function (result) {
            var zNodes = result.data;
            zTreeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
        });
    };
    // 点击节点回调
    function zTreeOnClick(event, treeId, treeNode) {
        $('#selectedName').html(treeNode.name);
        if (treeNode.id > 0) {
            $.post('/SysMenu/AjaxEntity', { id: treeNode.id }, function (result) {
                $('#dataForm').setForm(result.data);
                form.render();
            });
        } else {
            $('#btnReset').click();
            data = { ID: 0, Pid: 0, IsValid: true };
            $('#dataForm').setForm(data);
            form.render();
        }
    };
    // 保存当前节点
    function submitNode() {
        var data = $('#dataForm').getForm();
        if (data.IsValid)
            data.IsValid = true;
        else
            data.IsValid = false;
        if (!data.ID) {
            layer.msg('请选择一个节点');
            return;
        }
        editNode(data);
    };
    // 添加子节点
    function submitNodeChild() {
        var data = $('#dataForm').getForm();
        if (data.IsValid)
            data.IsValid = true;
        else
            data.IsValid = false;
        if (!data.ID) {
            layer.msg('请选择一个节点');
            return;
        }
        data.Pid = data.ID;
        data.ID = 0;
        editNode(data);
    };
    // 提交修改
    function editNode(data) {
        $.post('/SysMenu/AjaxEdit', data, function (result) {
            layer.msg(result.msg);
            bindTree();
        });
    };
    // 提交删除
    function deleteNode() {
        var data = $('#dataForm').getForm();
        if (!data.ID) {
            layer.msg('请选择一个节点');
            return;
        }

        var confirmWin = layer.confirm('确定删除数据？', {
            btn: ['确定', '取消'] //按钮
        }, function () {
            //此处添加执行过程
            layer.close(confirmWin);
            $.post('/SysMenu/AjaxDelete', { ids: data.ID }, function (result) {
                layer.msg(result.msg);
                bindTree();
            });
        }, function () {
            layer.close(confirmWin);
        });
    };
</script>